﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using mx.ipn.escom.tt20080015.modelo.codificacion;
using mx.ipn.escom.tt20080015.modelo.memoria;

namespace tt20080015.vistas.especialista
{
    public partial class CasoEstudio : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] == null)
                Server.Transfer("~/Default.aspx", false);

            if (Context.Items["paciente"] != null)
            {
                mx.ipn.escom.tt20080015.modelo.datos.Paciente p = (mx.ipn.escom.tt20080015.modelo.datos.Paciente)Context.Items["paciente"];
                lbPaciente.Text += p.Nombre;
                lbPaciente.Text += p.Id;
                idPacienteHiddenField.Value = "" + p.Id;

                mx.ipn.escom.tt20080015.modelo.db.CasoEstudioManager cem = new mx.ipn.escom.tt20080015.modelo.db.CasoEstudioManager();
                Mx.Ipn.Escom.tt20080015.modelo.datos.Estudio est = cem.consultar(p.Id);
                Response.Write("<script type=\"text/javascript\">alert('" + est.ToString() + "')</script>");//Quitar
                Response.Write("<script type=\"text/javascript\">alert('" + est.A() + "')</script>");//Quitar
                lClass.SelectedIndex = (int)est.Recurrencia;
                lAge.SelectedIndex = (int)est.Age;
                lMenopause.SelectedIndex = (int)est.Menopause;
                lTumorSize.SelectedIndex = (int)est.TumorSize;
                lInvNodes.SelectedIndex = (int)est.InvNodes;
                lNodeCaps.SelectedIndex = (int)est.NodeCaps;
                lDegMalig.SelectedIndex = (int)est.DegMalig;
                lBreast.SelectedIndex = (int)est.Breast;
                lBreastQuad.SelectedIndex = (int)est.Breast;
                lIrradiat.SelectedIndex = (int)est.Irradiat;
            }
            else
            {
                lbPaciente.Text = "";
            }
        }

        protected void btnClasificar_Click(object sender, EventArgs e)
        {
            // ASi no va esta mal y feo :)
            Parametro p = new Parametro();
            BAMAlphaBeta m = new BAMAlphaBeta();
            
            BinaryFormatter bformatter = new BinaryFormatter();
            Stream s = File.Open("c:\\test.kb", FileMode.Open);
            m = (BAMAlphaBeta)bformatter.Deserialize(s);
            s.Close();
            string[] parametros = {"0",
                                    lAge.SelectedValue,
                                    lMenopause.SelectedValue,
                                    lTumorSize.SelectedValue,
                                    lInvNodes.SelectedValue,
                                    lNodeCaps.SelectedValue,
                                    lDegMalig.SelectedValue,
                                    lBreast.SelectedValue,
                                    lBreastQuad.SelectedValue,
                                    lIrradiat.SelectedValue
                                  };
            int a = m.Output(p.obtenerTrama(parametros))[0];
            //Response.Redirect("~/vistas/especialista/Clasificacion.aspx");
            Context.Items.Add("value",a.ToString());// Para que enviamos todo, pero bueno.
            if(!idPacienteHiddenField.Value.Equals("0"))
            {
                int i = Convert.ToInt32(idPacienteHiddenField.Value);
                Mx.Ipn.Escom.tt20080015.modelo.datos.Estudio c = new Mx.Ipn.Escom.tt20080015.modelo.datos.Estudio(
                                        parametros[0],
                                        parametros[1],
                                        parametros[2],
                                        parametros[3],
                                        parametros[4],
                                        parametros[5],
                                        parametros[6],
                                        parametros[7],
                                        parametros[8],
                                        parametros[9], DateTime.Now.ToString("yyyy/MM/dd"), new DateTime().ToString("yyyy/MM/dd"), i);
                mx.ipn.escom.tt20080015.modelo.db.CasoEstudioManager cem = new mx.ipn.escom.tt20080015.modelo.db.CasoEstudioManager();
                cem.actualizar(c);
            }
            Server.Transfer("~/vistas/especialista/Clasificacion.aspx");
        }
    }
}
